﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL_Class;

namespace NLLSite.Administrator.UserControl
{
    public partial class ManageAuthor : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                     
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel2.Visible = true;
            btnXoa.Visible = false;
            cbxAll.Visible = false;
            Button3.Visible = false;

            Session["ID"] = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
            txtAuthor.Text = HttpUtility.HtmlDecode(GridView1.SelectedRow.Cells[2].Text);
            CKEditor1.Text = HttpUtility.HtmlDecode(((Label)GridView1.SelectedRow.FindControl("lblDescription")).Text);
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            TacGia tg = new TacGia();
            Author au = new Author();

            au.ID = Int32.Parse(Session["ID"].ToString());
            au.AuthorName = txtAuthor.Text;
            au.Description = CKEditor1.Text;
            tg.UpdateAuthor(au);
            Panel2.Visible = false;
            btnXoa.Visible = true;
            cbxAll.Visible = true;
            Button3.Visible = true;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Panel2.Visible = false;
            btnXoa.Visible = true;
            cbxAll.Visible = true;
            Button3.Visible = true;
        }

        protected void cbxAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("cbxDelete");
                if (cbxAll.Checked)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            TacGia tg = new TacGia();
            Author au = new Author();

            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("cbxDelete");
                if (cb != null && cb.Checked)
                {
                    au.ID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                    tg.DeleteAuthor(au);
                }
            }
            GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemTacGia.aspx");
        }
    }
}